package org.example.modelTest;

import org.eclipse.paho.client.mqttv3.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class TestMap {

    public static Map<String, String> parseMapForFilter(Map<String, String > map, String filters) {
        if (map == null) {
            return null;
        } else {
            if(filters==null||filters.isEmpty()){
                return map;
            }
            map = map.entrySet().stream()
                    .filter((e) -> checkKey(e.getKey(),filters))
                    .collect(Collectors.toMap(
                            (e) -> (String) e.getKey(),
                            (e) -> e.getValue()
                    ));
        }
        return map;
    }

    /**
     * 通过indexof匹配想要查询的字符
     */
    private static boolean checkKey(String key,String filters) {
        if (key.indexOf(filters)>-1){
            return true;
        }else {
            return false;
        }
    }
    public static void getList(Map<String, String> map, Integer current, Integer size){
        List<Object> list=new ArrayList<>();//所有数据Total个
        for(String key:map.keySet()){
            list.add(key);
        }
        System.out.println(list);
        int pages=(int)Math.ceil(map.size()*1.0/size);
        System.out.println(pages);
        //计算current页是多少个
        if(current>=pages){//最后一页[(current-1)*size-1,Total-1]
            System.out.println(list.subList((current-1)*size, map.size()));
        }
        else{//非最后一页[(current-1)*size-1,current*size-1]
            System.out.println(list.subList((current-1)*size,current*size));
        }
    }
    private static void decToBit(int dec){
        String s=Integer.toBinaryString(dec);
        char[] c=s.toCharArray();
        int[] binary=new int[c.length];
        int alarmNum=0;
        for(int i=0;i<c.length;i++){
            System.out.print(c[i]);
        }
        System.out.println();
        for(int i=0;i<c.length;i++){
            binary[i] = Integer.parseInt(String.valueOf(c[i]));
            System.out.print(binary[i]);
        }
        for(int i=0;i<c.length;i++){
            //统计alarm位数
            if(binary[i]==1){
                alarmNum++;
                //[i]实际上为第length-1-i位
                System.out.println("Error bit: "+(binary.length-1-i));
            }
        }
        System.out.println("\nalarmNum: "+alarmNum);
    }
    public static void getTs(String startTime,String endTime){
        long startTs=0;
        long endTs=0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = simpleDateFormat.parse(startTime);
            Date endDate=simpleDateFormat.parse(endTime);
            startTs=startDate.getTime();
            endTs=endDate.getTime();
        } catch (ParseException e) {
            System.out.println("Date format should be like yyyy-MM-dd HH:mm:ss");
        }
        if(startTs>=endTs){
            System.out.println("StartTime should be earlier than endTime");
        }
        System.out.println("startTs: "+startTs);
        System.out.println("endTs: "+endTs);
    }
    public static String setHexLowestBitTwo(String hexStr,int control) {
        int hex = Integer.parseInt(hexStr, 16);
        //控制倒数第二位bit1放电
        int newHex = (hex & 0xFFFD) | ((control & 1) << 1);
        System.out.println(Integer.toBinaryString(newHex));
//        String newHexStr=Integer.toHexString(newHex).toUpperCase();
//        while (newHexStr.length()<2){
//            newHexStr="0"+newHexStr;
//        }

        return String.format("%02X", newHex);
    }
    public  static String makeCheckSum(String data){
        if (data == null || data.equals("")) {
            return "";
        }
        int total = 0;
        int len = data.length();
        int num = 0;
        while (num < len) {
            String s = data.substring(num, num + 2);
//            System.out.println(s);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        //用256求余最大是255，即16进制的FF
        int mod = total % 256;
        String hex = Integer.toHexString(mod);
        len = hex.length();
        // 如果不够校验位的长度，补0,这里用的是两位校验
        if (len < 2) {
            hex = "0" + hex;
        }
        return hex.toUpperCase();

    }
    private static Double getKmDistance(Double distance){
        BigDecimal distanceBig=BigDecimal.valueOf(distance);
        BigDecimal thousand=BigDecimal.valueOf(1000);
        return distanceBig.divide(thousand).doubleValue();
    }
    // 整数转换为1个字节的十六进制
    private static String decimalToHex(int decimal) {
        String hex = Integer.toHexString(decimal);
        return hex.length() == 1 ? "0" + hex : hex;
    }
    public static String cutPre(String str){
        if(str==null){
            return null;
        }
        char first=str.charAt(0);
        if(first=='N'){
            str=str.substring(1,str.length());
        }
        return str;
    }
    public static String[] preprocess(String hexString){

        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString=cutPre(hexString);
        hexString = hexString.trim().replaceAll("\\s*", ""); // 去除字符串中的空格
        hexString=hexString.replaceAll("N","");
        hexString=hexString.toUpperCase();// 大写
//        System.out.println(hexString);
        String[] code=new String[hexString.length()/2];
        int i=0;

        try{
            while(true){
                code[i]=hexString.substring(0,2);//取前两个字符
//            System.out.println("code"+i+" "+code[i]);
                i++;
                hexString=hexString.substring(2,hexString.length());
                if(hexString == null || hexString.equals("")){
                    break;
                }
            }
        }
        catch (StringIndexOutOfBoundsException e){
            return null;
        }
//        for(String s:code){
//            System.out.print(s);
//        }
//        System.out.println();
        return code;
    }
    public static String dealHexAscii(String hexAscii){
        String[] code=preprocess(hexAscii);
        String resultStr="";
        for(int i=0;i<code.length;i++){
            resultStr+=Character.toString((char) Integer.parseInt(code[i],16));
        }
        return resultStr;
    }

    public static void main(String[] args) throws MqttException {
        Map<String, String> ImeiCustomerTitleTemp= new HashMap<>();
        ImeiCustomerTitleTemp.put("a","1");
        ImeiCustomerTitleTemp.put("a","2");
        System.out.println("size:"+ImeiCustomerTitleTemp.size());
        for(String key :ImeiCustomerTitleTemp.keySet()){
            String value=ImeiCustomerTitleTemp.get(key);
            System.out.println(key+":"+value);
        }



    }


}
